/**
 * 创建 game-box 窗体
 */
function makeGameBox (){
    "use strict";
    let gameBox = document.getElementById("game-box");
    gameBox.className = "game-box";
    gameBox.style.height = document.body.scrollHeight- 40 + "px";
    return gameBox;
}

function initGame(box, start) {
    "use strict";
    let newGame = document.createElement("div");
    newGame.style.position = "fixed";
    newGame.style.top = "40px";
    newGame.style.left = 0;
    newGame.style.width = document.body.clientWidth+ "px";
    newGame.style.height = document.body.scrollHeight- 40 + "px";
    newGame.style.background = "black";
    newGame.style.lineHeight = document.body.scrollHeight- 40 + "px";
    newGame.style.textAlign = "center";
    newGame.style.color = "white";
    newGame.innerHTML = "<h1>点击开始游戏</h1>";
    console.log(newGame);
    newGame.addEventListener("click", function(event) {
        start();
    });
    box.appendChild(newGame);
}

function addRole(img) {
    "use strict";
    let role = document.createElement("div");
    role.className = "role";
    role.style.left = document.body.clientWidth / 2 - 25 /2+ "px";
    role.style.top = document.body.scrollHeight - 200+ "px";
    role.style.background = "url("+img +") no-repeat center";
    return role;
}

function makeWall(left) {
    "use strict";
    let wall = document.createElement("div");
    wall.className = "wall";
    wall.style.left = left+ "px";
    return wall;
}

function finalPlayer(box) {
    "use strict";
    let msg = document.createElement("div");
    msg.style.width = "100%";
    msg.style.height = document.body.scrollHeight- 40 + "px";
    msg.style.position = "absolute";
    msg.style.top = "40px";
    msg.style.left = 0;
    msg.style.background = "rgba(0,0,0,0.5)";

    let hh = document.createElement("div");
    let wd = document.body.clientWidth * 3 / 5;
    hh.style.width = wd+ "px";
    hh.style.height = "100px";
    hh.style.position = "absolute";
    hh.style.top = "30%";
    hh.style.left = "50%";
    let swap = wd/2;
    hh.style.marginLeft = "-"+ swap+ "px";
    hh.style.background = "white";
    hh.style.lineHeight = "100px";
    hh.style.textAlign = "center";
    hh.innerText = "GAME OVER";
    msg.appendChild(hh);
    box.appendChild(msg);

    msg.addEventListener("click", function(event) {
        window.start();
    });
}

function wallMove(box, wall, randLeft) {
    "use strict";
    var wth = document.body.clientWidth / 10;
    var hit = 100;
    var wallRight = randLeft + wth;
    var wallLeft  = randLeft;
    box.appendChild(wall);
    var swapTop = 40;
    var swapBot = 140;
    window.interV.push(setInterval(function() {
        // 墙的右边大于等于飞机的左边并且墙的左边小于飞机的右边
        if (wallRight >= window.state.pst.left && wallLeft < window.state.pst.left+ 25) {
            // 墙的上边小于等于飞机的下边并且墙的下边大于飞机的上边
            if (swapTop <= window.state.pst.top + 65 && swapTop+ 60 > window.state.pst.top) {
                clearInterval(window.wallFac);
                clearInterval(window.ctrl);
                console.log(window.interV);
                for (var i = 0; i < window.interV.length; i++) {
                    clearInterval(window.interV[i]);
                }
                finalPlayer(box);
            }
        }
        swapTop += 1;
        wall.style.top = swapTop+ "px";
        if (swapTop >= document.body.scrollHeight) {
            try {
                wall.parentNode.removeChild(wall);
            } catch(e) {
                console.log(e);
                return 0;
            }
            clearInterval(window.interV[window.vCount]);
            window.vCount++;
        }
    }, 10));
}